﻿(function ($) {
    $.extend({
        thickbox: function (url, caption) {
            this.url = url;
            this.getParams = function () {
                var url = this.url;
                var params = { width: 630, height: 440, modal: 0 };
                var array = url.split('@');
                params.target = array[0];
                params.type = array[1].split('/')[0];
                params.width = array[1].split('/')[1];
                params.height = array[1].split('/')[2];
                params.modal = array[1].split('/')[3];
                return params;
            };

            this.detectMacXFF = function () {
                var userAgent = navigator.userAgent.toLowerCase();
                if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox') != -1) {
                    return true;
                }
                return false;
            };

            var params = this.getParams();
            var width = (params.width * 1) + 30 || 630;
            var height = (params.height * 1) + 40 || 440;
            var contentWidth = width - 16;
            var contentHeight = height - 45;
            if (document.getElementById('thickbox-body') === null) {
                var headerstr = $.format("<div class='thickbox-header'><div class='thickbox-title'>{0}</div>{1}</div>", caption, params.modal == 0 ? $.format("<div class='thickbox-close'><a href='javascript:void(0);' title='关闭'>&nbsp;&nbsp;&nbsp;&nbsp;</a></div>") : "");
                var containerstr = $.format("<div class='thickbox-container' style='width:{0}px;height:{1}px'></div>", contentWidth, contentHeight);
                var thickboxhtml = $.format("<div id='thickbox-body' class='thickbox-body'><div class='{0}'></div><div class='thickbox-main'>{1}{2}</div></div>", this.detectMacXFF() ? "thickbox-maskhack" : "thickbox-mask", headerstr, containerstr);
                $(thickboxhtml).appendTo(document.body);
            }
            var thickbox = $(document.getElementById('thickbox-body'));

            var thickboxBody = thickbox.children(".thickbox-main");

            thickboxBody.css({ marginLeft: '-' + parseInt((width / 2), 10) + 'px', width: width + 'px' });
            if (!(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
                thickboxBody.css({ marginTop: '-' + parseInt((height / 2), 10) + 'px' });
            }

            thickboxBody.find('.thickbox-title').html(caption);
            thickboxBody.show();

            var thickboxContainer = thickboxBody.children(".thickbox-container").empty();

            if (params.type == 1) {
                var temp = document.getElementById(params.target).innerHTML;
                thickboxContainer.append(temp).show();
            }
            else if (params.type == 2) {
                thickboxContainer.load(params.target, function () {
                    thickboxContainer.show();
                    $(".thickbox", ".thickbox-container").thickbox();
                });
            }
            else if (params.type == 3) {
                $($.format("<iframe src='{0}' frameBorder='0' scrolling='auto' style='width:{1}px;height:{2}px;'></iframe>", params.target, contentWidth, contentHeight)).appendTo(thickboxContainer);
            }

            if (params.modal == 0) {
                if (thickboxBody.find(".thickbox-close>a").size() < 1) {
                    var close = "<div class='thickbox-close'><a href='javascript:void(0);' title='关闭'>&nbsp;&nbsp;&nbsp;&nbsp;</a></div>";
                    thickboxBody.find('.thickbox-header').append(close);
                }
                thickboxBody.find(".thickbox-close>a").show().click(function () {
                    thickboxContainer.empty();
                    thickbox.fadeOut('slow', function () { thickbox.remove(); });
                });
                thickbox.find(".silenthink-mask").click(function () {
                    thickbox.fadeOut('slow', function () { thickbox.remove(); });
                });
            }
            if (params.modal == 1) {
                thickboxBody.find(".thickbox-close>a").remove();
                thickbox.find(".silenthink-mask").unbind();
            }

            $(document).keyup(function (event) {
                if (event.keyCode == 27 && params.modal == 0) {
                    thickbox.fadeOut('slow', function () { thickbox.remove(); });
                }
            });
        }
    });
    $.fn.thickbox = function () {
        $(this).click(function () {
            var caption = this.title || this.name || '';
            var url = this.href || this.alt;
            $.thickbox(url, caption);
            this.blur();
            return false;
        });
    };
})(jQuery);
$(function () { $('.thickbox').thickbox(); });